//
//  ViewController.m
//  项目一
//
//  Created by 王鑫 on 15/11/12.
//  Copyright (c) 2015年 王鑫. All rights reserved.
//

#import "ViewController.h"
#import "LatestCollectionViewCell.h"
#import "LatestDetailViewController.h"
#import "VideoCollectionViewCell.h"
#import "NewCollectionViewCell.h"
#import "GuideCollectionViewCell.h"
#import "MyCollectionReusableView.h"
#import "VideoDetailViewController.h"
#import "GuideDetailViewController.h"

#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height


@interface ViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, VideoCollectionViewCellDelegate, GuideCollectionViewCellDelegate, LatestCollectionViewCellDelegate>

@property(nonatomic, retain) NSArray *arr;
@property(nonatomic, retain) UIScrollView *titleScrollerView;
@property(nonatomic, retain) UICollectionView *collectionView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor redColor];
    self.navigationController.navigationBar.translucent = NO;
    // 创建上面的小题目(4个)
    self.arr = @[@"最新", @"视频", @"快报", @"导购"];
    self.titleScrollerView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 30)];
    // 设置navigation的题目是创建的titleScrollerView
    self.navigationItem.titleView = self.titleScrollerView;
    for (NSInteger i = 0; i < self.arr.count; i++) {
        UILabel *smallTitleView = [[UILabel alloc] initWithFrame:CGRectMake(90 * i, 0, 90, 30)];
        smallTitleView.text = self.arr[i];
        [self.titleScrollerView addSubview:smallTitleView];
        [smallTitleView release];
        
        // 给每个label设个tag值方便我们找到它
        smallTitleView.tag = 1000 + i;
        // 打开用户交互
        smallTitleView.userInteractionEnabled = YES;
        // 给每个Label添加一个点击事件
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(click:)];
        [smallTitleView addGestureRecognizer:tap];
        
    }
    UILabel *tempLabel = (UILabel *)[self.titleScrollerView viewWithTag:1000];
    tempLabel.textColor = [UIColor redColor];
    
    [_titleScrollerView release];
    self.titleScrollerView.contentSize = CGSizeMake(self.arr.count * 90, 0);
    self.titleScrollerView.contentOffset = CGPointMake(0, 0);
    
    // 创建UICollectionView
    UICollectionViewFlowLayout *flowlayout = [[UICollectionViewFlowLayout alloc] init];
    flowlayout.itemSize = CGSizeMake(WIDTH, HEIGHT);
    flowlayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flowlayout.minimumInteritemSpacing = 0;
    flowlayout.minimumLineSpacing = 0;
    flowlayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) collectionViewLayout:flowlayout];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_collectionView];
    [self.collectionView release];
    self.collectionView.pagingEnabled = YES;
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    // 注册cell
    [self.collectionView registerClass:[LatestCollectionViewCell class] forCellWithReuseIdentifier:@"latestCollectionReuse"];
    [self.collectionView registerClass:[VideoCollectionViewCell class] forCellWithReuseIdentifier:@"videoCollectionReuse"];
    [self.collectionView registerClass:[NewCollectionViewCell class] forCellWithReuseIdentifier:@"newCollectionReuse"];
    [self.collectionView registerClass:[GuideCollectionViewCell class] forCellWithReuseIdentifier:@"guideCollectionReuse"];
}
#pragma mark - Method number Of Items In Section
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.arr.count;
}
#pragma mark - Method cell For Item At IndexPath
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        LatestCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"latestCollectionReuse" forIndexPath:indexPath];
        cell.contentView.backgroundColor = [UIColor yellowColor];
        cell.latestDelegate = self;
        return cell;
    } else if (indexPath.row == 1) {
        VideoCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"videoCollectionReuse" forIndexPath:indexPath];
        cell.contentView.backgroundColor = [UIColor cyanColor];
        // 页面进行传值时, 使用的
        // 设置代理人
        cell.delegate = self;
        return cell;
    } else if (indexPath.row == 2) {
        NewCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"newCollectionReuse" forIndexPath:indexPath];
        cell.contentView.backgroundColor = [UIColor redColor];
        return cell;
    } else if (indexPath.row == 3) {
        GuideCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"guideCollectionReuse" forIndexPath:indexPath];
        cell.contentView.backgroundColor = [UIColor magentaColor];
        cell.delegate = self;
        return cell;
    } else {
        LatestCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse0" forIndexPath:indexPath];
        cell.contentView.backgroundColor = [UIColor yellowColor];
        return cell;
    }
}
#pragma mark - Method  scrollViewDidEndDecelerating
// 这个就是当滑动结束 执行的方法
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    // 通过使用tag值, 来找到当前在scrollerView上的label
    UILabel *tempLabel = (UILabel *)[self.titleScrollerView viewWithTag:1000];
    UILabel *tempLabel1 = (UILabel *)[self.titleScrollerView viewWithTag:1001];
    UILabel *tempLabel2 = (UILabel *)[self.titleScrollerView viewWithTag:1002];
    UILabel *tempLabel3 = (UILabel *)[self.titleScrollerView viewWithTag:1003];
    if (scrollView == self.collectionView) {
        // 判断collectionView的偏移量
        if (self.collectionView.contentOffset.x == WIDTH) {
            tempLabel.textColor = [UIColor blackColor];
            tempLabel1.textColor = [UIColor redColor];
            tempLabel2.textColor = [UIColor blackColor];
            tempLabel3.textColor = [UIColor blackColor];
        }
        if (self.collectionView.contentOffset.x == WIDTH * 2) {
            tempLabel.textColor = [UIColor blackColor];
            tempLabel1.textColor = [UIColor blackColor];
            tempLabel2.textColor = [UIColor redColor];
            tempLabel3.textColor = [UIColor blackColor];
        }
        if (self.collectionView.contentOffset.x == WIDTH * 3) {
            tempLabel.textColor = [UIColor blackColor];
            tempLabel1.textColor = [UIColor blackColor];
            tempLabel2.textColor = [UIColor blackColor];
            tempLabel3.textColor = [UIColor redColor];
        }
        if (self.collectionView.contentOffset.x == 0) {
            tempLabel.textColor = [UIColor redColor];
            tempLabel1.textColor = [UIColor blackColor];
            tempLabel2.textColor = [UIColor blackColor];
            tempLabel3.textColor = [UIColor blackColor];
        }
        
    }

}
#pragma mark - Method click
// 这个是通过点击事件 找到当前点击的视图, 改变对应的self.collectionView的偏移量
- (void)click:(UITapGestureRecognizer *)tap
{
    UILabel *tempLabel = (UILabel *)[self.titleScrollerView viewWithTag:1000];
    UILabel *tempLabel1 = (UILabel *)[self.titleScrollerView viewWithTag:1001];
    UILabel *tempLabel2 = (UILabel *)[self.titleScrollerView viewWithTag:1002];
    UILabel *tempLabel3 = (UILabel *)[self.titleScrollerView viewWithTag:1003];
    // 点击的偏移量发生偏转
    if (tap.view == (UILabel *)[self.titleScrollerView viewWithTag:1001]) {
        self.collectionView.contentOffset = CGPointMake(WIDTH, 0);
        tempLabel1.textColor = [UIColor redColor];
        tempLabel2.textColor = [UIColor blackColor];
        tempLabel3.textColor = [UIColor blackColor];
        tempLabel.textColor = [UIColor blackColor];
    }
    if (tap.view == (UILabel *)[self.titleScrollerView viewWithTag:1002]) {
        self.collectionView.contentOffset = CGPointMake(WIDTH * 2, 0);
        tempLabel1.textColor = [UIColor blackColor];
        tempLabel2.textColor = [UIColor redColor];
        tempLabel3.textColor = [UIColor blackColor];
        tempLabel.textColor = [UIColor blackColor];
    }
    if (tap.view == (UILabel *)[self.titleScrollerView viewWithTag:1003]) {
        self.collectionView.contentOffset = CGPointMake(WIDTH * 3, 0);
        tempLabel1.textColor = [UIColor blackColor];
        tempLabel2.textColor = [UIColor blackColor];
        tempLabel3.textColor = [UIColor redColor];
        tempLabel.textColor = [UIColor blackColor];
    }
    if (tap.view == (UILabel *)[self.titleScrollerView viewWithTag:1000]) {
        self.collectionView.contentOffset = CGPointMake(0, 0);
        tempLabel1.textColor = [UIColor blackColor];
        tempLabel2.textColor = [UIColor blackColor];
        tempLabel3.textColor = [UIColor blackColor];
        tempLabel.textColor = [UIColor redColor];
    }
}
#pragma mark - Method VideoPushToWebView
// 协议中的一步, 将页面进行跳转, 在viewController里面的model
- (void)VideoPushToWebView:(VideoModel *)model {
    VideoDetailViewController *detailVC = [[VideoDetailViewController alloc] init];
    detailVC.model = model;
    [self.navigationController pushViewController:detailVC animated:YES];
    [detailVC release];
}
- (void)GuidePushToWeb:(GuideModel *)model {
    GuideDetailViewController *detailVC = [[GuideDetailViewController alloc] init];
    detailVC.model = model;
    [self.navigationController pushViewController:detailVC animated:YES];
    [detailVC release];
}
- (void)LatestPushToWeb:(LatestModel *)model {
    LatestDetailViewController *detailVC = [[LatestDetailViewController alloc] init];
    detailVC.model = model;
    [self.navigationController pushViewController:detailVC animated:YES];
    [detailVC release];
}
#pragma mark - Method didReceiveMemoryWarning
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
